#include "StdAfx.h"
//-------------------------------------------------------------------------------------------------
//ImageData.cpp  - Information in an ImageRec corresponding to an Ultrasound Image          
//-------------------------------------------------------------------------------------------------
//FreeBSD License
//Copyright 2011 Will Stott All rights reserved.
//
//Redistribution and use in source and binary forms, with or without modification, are
//permitted provided that the following conditions are met:
//
//   1. Redistributions of source code must retain the above copyright notice, this list of
//      conditions and the following disclaimer.
//
//   2. Redistributions in binary form must reproduce the above copyright notice, this list
//      of conditions and the following disclaimer in the documentation and/or other materials
//      provided with the distribution.
//
//THIS SOFTWARE IS PROVIDED BY Will Stott ``AS IS'' AND ANY EXPRESS OR IMPLIED
//WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
//FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Will Stott OR
//CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
//CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
//SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
//ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
//NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
//ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
//The views and conclusions contained in the software and documentation are those of the
//authors and should not be interpreted as representing official policies, either expressed
//or implied, of Will Stott.
//
//----Authors--------------------------------------------------------------------------------------
//created by: will.stott@ucl.ac.uk Feb 2011
//
//----Comments-------------------------------------------------------------------------------------
//
//-------------------------------------------------------------------------------------------------

#include <iostream>
#include <fstream>

#include "ImageData.h"
#include "DicomFile.h"


ImageData::ImageData(void)
{
	_StoredBy = L"?";
	_pDicomFile = new DicomFile();
	_DbErrorCode = DBSTATUS_S_OK;
	_pBitMap = NULL;
}

ImageData::~ImageData(void)
{
	delete _pDicomFile;
	if (_pBitMap != NULL)
		delete _pBitMap;
}

const TCHAR *ImageData::GetErrorMsg()
{
	if ( _DbErrorCode == ImageData::LengthMismatch )
		return L"Number of bytes stored in database not equal to number of bytes in original file";
	if ( _DbErrorCode == ImageData::DicomError )
		return L"Failed to initialise from Dicom File";
	if ( _DbErrorCode == ImageData::BitMapCreateError)
		return L"Failed to create bitmap";

	if ( _DbErrorCode == DBSTATUS_S_OK )
		return L"no error";
	if ( _DbErrorCode == DBSTATUS_S_TRUNCATED )
		return L"number of bytes stored in database is greater than read buffer size";
	if ( _DbErrorCode == DBSTATUS_E_CANTCONVERTVALUE )
		return L"canot properly convert datatype in database to datatype in buffer";

	return L"undefined error";
}

int ImageData::CompareFiles(const wchar_t *GoodFile,const wchar_t *BadFile)
{
	using namespace std;

	int diffCnt = -1;

	BYTE *okBuff = '\0';
	BYTE *nokBuff = '\0';
	
	ifstream fileOK(GoodFile, ios::in| ios::binary);  
	ifstream fileNOK(BadFile, ios::in | ios::binary);

	long fileOKLength = -1;
	long fileNOKLength = -1;

	if (fileOK.is_open())
	{
		std::streamoff  begin = fileOK.tellg();
		fileOK.seekg (0, ios::end);
		std::streamoff end = fileOK.tellg();
//#pragma warning(disable:4244)
		fileOKLength = end - begin;
//#pragma warning(default:4244)
		fileOK.seekg (0, ios::beg);

		okBuff = (BYTE *) malloc(fileOKLength + 1);

		if ( fileOK.read((char *)okBuff, fileOKLength))
		{
			std::streamsize okCnt = fileOK.gcount(); 
			if (okCnt == fileOKLength)
				*(okBuff + fileOKLength) = 0x55;
		}
		fileOK.close();
	}

	if (fileNOK.is_open())
	{
		std::streamoff begin = fileNOK.tellg();
		fileNOK.seekg (0, ios::end);
		std::streamoff end = fileNOK.tellg();
//#pragma warning(disable:4244)
		fileNOKLength = end - begin;
//#pragma warning(default:4244)
		fileNOK.seekg (0, ios::beg);

		nokBuff = (BYTE *) malloc(fileNOKLength + 1);

		if ( fileNOK.read((char *)nokBuff, fileNOKLength) )
		{
			std::streamsize nokCnt = fileNOK.gcount(); 
			if (nokCnt == fileNOKLength)
				*(nokBuff + fileNOKLength) = 0x55;
		}
		fileNOK.close();
	}

	BYTE *pOk = okBuff;
	BYTE *pNOk = nokBuff;

	if ((fileOKLength >= fileNOKLength) && (fileOKLength > 0))
	{
		diffCnt = 0;
		for (int cnt = 0; cnt < fileNOKLength; cnt++ )
		{
			if (*pOk != *pNOk )
			{
				diffCnt++;
			}
			pOk++;
			pNOk++;
		}
	}
	if ( nokBuff != '\0' )
		free (nokBuff);
	if ( okBuff != '\0' )
		free (okBuff);

	return diffCnt;
}



//-------------------------------------------------------------------------------------------------
//			E N D   O F   F I L E
//-------------------------------------------------------------------------------------------------
